package com.dotstone.chipism.adapter;

import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Scene_Task;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TaskAdapter extends BaseAdapter {
	private Context context;
	private List<Scene_Task> data;
	private Handler mHandler;
	int aaa;

	public TaskAdapter(Context context) {
		this.context = context;
	}

	public TaskAdapter(Context context, List<Scene_Task> data ,int aaa) {
		this.data = data;
		this.context = context;
		this.aaa = aaa;
	}

	public void setHandler(Handler mHandler) {
		this.mHandler = mHandler;
	}

	public void setData(List<Scene_Task> data) {
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void clear() {
		this.data.clear();
		notifyDataSetChanged();
	}

	public void addAll(List<Scene_Task> data) {
		if (data != null) {
			this.data.addAll(data);
			notifyDataSetChanged();
		}
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		if (view == null) {
			view = LayoutInflater.from(context).inflate(R.layout.item_task,
					null);
			holdView = new HoldView();
			holdView.task_num_t = (TextView) view
					.findViewById(R.id.task_num_tv);
			holdView.mDeleteB = (Button) view.findViewById(R.id.delete_btn);
			holdView.mDelayl = (LinearLayout) view.findViewById(R.id.delay_l);
			holdView.type_t = (TextView) view.findViewById(R.id.type_t);
			holdView.task_t = (TextView) view.findViewById(R.id.task_tv);
			holdView.hour_t = (TextView) view.findViewById(R.id.hour_tv);
			holdView.min_t = (TextView) view.findViewById(R.id.min_tv);
			holdView.sec_t = (TextView) view.findViewById(R.id.sec_tv);
			view.setTag(holdView);
		} else {
			holdView = (HoldView) view.getTag();
		}
		Scene_Task task = data.get(position);
		holdView.task_num_t.setText(context.getString(R.string.task_name)
				+ (position + 1) + ":");
		//holdView.type_t.setText(task.getDeviceName());
		//holdView.task_t.setText(task.getCmdName().split("\\|")[0]);
		if(task.getCmdName().contains("|")){
			holdView.type_t.setText(task.getCmdName().split("\\|")[1]);
			holdView.task_t.setText(task.getCmdName().split("\\|")[0]);
		}else {
			holdView.type_t.setText(task.getDeviceName());
			holdView.task_t.setText(task.getCmdName());
		}
		holdView.hour_t.setText(task.getHour() + "");
		holdView.min_t.setText(task.getMin() + "");
		holdView.sec_t.setText(task.getSec() + "");
		holdView.mDeleteB.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Message msg = Message.obtain();
				msg.what = 0;
				msg.arg1 = position;
				mHandler.sendMessage(msg);
			}
		});
		return view;
	}

	HoldView holdView;

	class HoldView {
		private TextView task_num_t, type_t, task_t, hour_t, min_t, sec_t;
		private LinearLayout mDelayl;
		private Button mDeleteB;
	}

}
